﻿// See https://aka.ms/new-console-template for more information
//给你一个下标从 0 开始的二维整数数组 flowers ，其中 flowers[i] = [starti, endi] 表示第 i 朵花的 花期 从 starti 到 endi （都 包含）。同时给你一个下标从 0 开始大小为 n 的整数数组 people ，people[i] 是第 i 个人来看花的时间。
//请你返回一个大小为 n 的整数数组 answer ，其中 answer[i] 是第 i 个人到达时在花期内花的 数目 。
//https://leetcode.cn/problems/number-of-flowers-in-full-bloom/
using System.Diagnostics;

int[][] flowers = new int[][] { new int[] { 1, 6 }, new int[] { 3, 7 }, new int[] { 9, 12 }, new int[] { 4, 13 } };
int[] people = new int[] { 2, 3, 7, 11 };

Stopwatch sw = Stopwatch.StartNew();
sw.Start();
var result = FullBloomFlowers2(flowers, people);
foreach (var item in result)
{
    Console.WriteLine(item);
}
sw.Stop();
Console.WriteLine("耗时"+sw.ElapsedMilliseconds);

//解法1，双层循环穷举，超时
int[] FullBloomFlowers(int[][] flowers, int[] people)
{
    int[] result = new int[people.Length];
    int i = 0;
    foreach (var item1 in people)
    {
        foreach (var item2 in flowers)
        {
            if (item1 >= item2[0] && item1 <= item2[1])
            {
                result[i] += 1;
            }
        }
        i++;
    }
    return result;
}

int[] FullBloomFlowers2(int[][] flowers, int[] people)
{
    int[] result = new int[people.Length];
    var leftArry=flowers.Select(x => x[0]).ToList();
    var rigthArry = flowers.Select(x => x[1]).ToList();
    int i = 0;
    foreach(var item in people)
    {
        result[i] = leftArry.Where(x=> x <=item ).Count()-rigthArry.Where(x=>x>item).Count();
        i++;
    }
    return result;
}